package com.lucien.system.service.impl;

import com.lucien.common.dto.RegistBody;
import com.lucien.common.dto.UserUpdate;
import com.lucien.common.utils.StringUtils;
import com.lucien.system.domain.*;
import com.lucien.system.mapper.SysRolesMapper;
import com.lucien.system.mapper.SysUserMapper;
import com.lucien.system.mapper.SysUsersRolesMapper;
import com.lucien.system.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

//import org.apache.shiro.SecurityUtils;

/**
 * @Author Lucien
 * @Date 2021/4/28 18:30
 * @Version 1.0
 * @Description:
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysUsersRolesMapper usersRolesMapper;

    @Autowired
    private SysRolesMapper rolesMapper;

    /**
     * 用户注册
     * @param registBody
     * @return
     */
    @Override
    public int userRegistere(RegistBody registBody) {
        if (checkUsername(registBody.getUsername())) {
            return -2;
        }
        SysUser user = new SysUser();
        user.setUsername(registBody.getUsername());
        //spring自带md5加密
        user.setPassword(DigestUtils.md5DigestAsHex(registBody.getPassword().getBytes()));
        user.setUserStateId(2);
        user.setIsLocked(false);
        user.setIsEnabled(true);
        user.setGender(registBody.getGender());
        user.setEmail(registBody.getEmail());

        SysUsersRoles usersRoles = new SysUsersRoles();

        if (userMapper.insert(user) != 1){
            return -1;
        }

        SysUser sysUser = selectByUsername(registBody.getUsername());
        usersRoles.setUserId(Long.valueOf(sysUser.getId()));
        usersRoles.setRoleId(Long.valueOf(2));
        if (usersRolesMapper.insert(usersRoles) != 1){
            return -1;
        }

        return 1;
    }

    /**
     * 用户名查询
     * @param username
     * @return
     */
    @Override
    public SysUser selectByUsername(String username) {
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andUsernameEqualTo(username);

        List<SysUser> userList = userMapper.selectByExample(sysUserExample);

        if (userList.size() == 0){
            return null;
        }

        return userList.get(0);
    }

    /**
     * 用户ID查询
     * @param userId
     * @return
     */
    @Override
    public SysUser selectByUserId(Integer userId) {
        SysUser sysUser = userMapper.selectByPrimaryKey(userId);
        if (StringUtils.isNull(sysUser)){
            return null;
        }
        return sysUser;
    }

    /**
     * 更新用户信息
     * @param userUpdate
     * @return
     */
    @Override
    public int updateUser(UserUpdate userUpdate) {
        SysUser sysUser = selectByUsername(userUpdate.getUsername());
        sysUser.setEmail(userUpdate.getEmail());
        sysUser.setGender(userUpdate.getGender());

        if (userMapper.updateByPrimaryKey(sysUser) != 1){
            return -1;
        }
        return 1;
    }

    /**
     * 禁用用户
     * @param id
     * @return
     */
    @Override
    public int disableUser(Integer id) {
        SysUser sysUser = userMapper.selectByPrimaryKey(id);
        if (StringUtils.isNull(sysUser)){
            return -1;
        }
        sysUser.setIsEnabled(false);
        sysUser.setIsLocked(true);

        if (userMapper.updateByPrimaryKey(sysUser) != 1){
            return -1;
        }
        return 1;
    }

    /**
     * 启用用户
     * @param id
     * @return
     */
    @Override
    public int enableUser(Integer id) {
        SysUser sysUser = userMapper.selectByPrimaryKey(id);
        if (StringUtils.isNull(sysUser)){
            return -1;
        }
        sysUser.setIsEnabled(true);
        sysUser.setIsLocked(false);

        if (userMapper.updateByPrimaryKey(sysUser) != 1){
            return -1;
        }
        return 1;
    }

    /**
     * 获取用户角色信息
     * @param username
     * @return
     */
    @Override
    public SysRoles getRole(String username) {
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andUsernameEqualTo(username);

        List<SysUser> userList = userMapper.selectByExample(sysUserExample);
        if (userList.size() == 0) {
            return null;
        }
        SysUser user = userList.get(0);
        SysUsersRolesExample rolesExample = new SysUsersRolesExample();
        SysUsersRolesExample.Criteria rolesExampleCriteria = rolesExample.createCriteria();
        rolesExampleCriteria.andUserIdEqualTo(Long.valueOf(user.getId()));

        List<SysUsersRoles> usersRoles = usersRolesMapper.selectByExample(rolesExample);

        SysRoles sysRoles = rolesMapper.selectByPrimaryKey(usersRoles.get(0).getRoleId());

        return sysRoles;
    }

    /**
     * 更新用户头像
     * @param username
     * @param path
     * @return
     */
    @Override
    public int updateProfile(String username, String path) {
        SysUser sysUser = selectByUsername(username);
        if (StringUtils.isNull(sysUser)) {
            return -1;
        }

        sysUser.setUserProfile(path);

        if (userMapper.updateByPrimaryKey(sysUser) != 1){
            return -1;
        }


        return 1;
    }

    /**
     * 修改用户状态
     * @param username
     * @param status
     * @return
     */
    @Override
    public int updateUserStatus(String username, Integer status) {
        SysUser sysUser = selectByUsername(username);
        sysUser.setUserStateId(status);

        if (userMapper.updateByPrimaryKey(sysUser) != 1){
            return -1;
        }

        return 1;
    }

    /**
     * 更新用户盐值
     * @param username
     * @param salt
     * @return
     */
    @Override
    public int updateUserSalt(String username, String salt) {
        SysUser sysUser = selectByUsername(username);
        sysUser.setSalt(salt);

        if (userMapper.updateByPrimaryKey(sysUser) != 1){
            return -1;
        }
        return 1;
    }

    /**
     * 获取用户状态
     * @param userId
     * @return
     */
    @Override
    public int getUserStatus(Integer userId) {
        SysUser sysUser = userMapper.selectByPrimaryKey(userId);

        if (StringUtils.isNull(sysUser)) {
            return -1;
        }

        return sysUser.getUserStateId();
    }

    /**
     * 获取用户角色信息
     * @param userId
     * @return
     */
    @Override
    public String getUserRoles(Integer userId) {
        SysUsersRolesExample sysUsersRolesExample = new SysUsersRolesExample();
        SysUsersRolesExample.Criteria criteria = sysUsersRolesExample.createCriteria();
        criteria.andUserIdEqualTo(Long.valueOf(userId));

        List<SysUsersRoles> sysUsersRoles = usersRolesMapper.selectByExample(sysUsersRolesExample);
        if (sysUsersRoles.size() == 0){
            return "用户ID错误";
        }
        SysUsersRoles usersRoles = sysUsersRoles.get(0);

        SysRoles sysRoles = rolesMapper.selectByPrimaryKey(usersRoles.getRoleId());

        return sysRoles.getRole();
    }

    /**
     * 修改密码
     * @param password
     * @return
     */
    @Override
    public int updatePassword(String password) {
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        loginUser.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));

        if (userMapper.updateByPrimaryKey(loginUser) != 1){
            return -1;
        }
        return 1;
    }

    /**
     * 检查用户名是否有相同
     * @param username
     * @return true:已注册 false:未注册
     */
    private boolean checkUsername(String username){
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);

        List<SysUser> sysUsers = userMapper.selectByExample(example);
        if (sysUsers.size() == 0){
            return false;
        }
        return true;
    }

    /**
     * 检查邮箱是否有相同
     * @param email
     * @return
     */
    private boolean checkEmail(String email){
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(email);

        List<SysUser> sysUsers = userMapper.selectByExample(example);
        if (sysUsers.size() != 0){
            return false;
        }
        return true;
    }
}
